package com.example.demo;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.io.IOException;
import java.util.concurrent.*;

/**
 * @author yishuangshuang
 * @ClassName: ExecutorServiceDemo
 * @Description: TODO
 * @date 2019/1/18 2:12 PM
 */
public class ExecutorServiceDemo {


    public static void main(String[] args) throws IOException {

        // 线程池不允许使用Executors创建，使用ThreadPoolExecutor

        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());


        ThreadFactory namedThreadFactory =
                new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();

        ExecutorService pool = new ThreadPoolExecutor(5, 200,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<>(1024),
                namedThreadFactory,
                new ThreadPoolExecutor.AbortPolicy());
        pool.execute(() -> System.out.println(Thread.currentThread().getName()));
        for (int i = 0; i < 20; i++) {
            System.out.println(i);
            pool.execute(() -> System.out.println(Thread.currentThread().getName()));

        }
        pool.shutdown();

        System.in.read();
    }
}
